/*
package org.pearl.oauth2.authorizationserverdemo.controller;

import lombok.RequiredArgsConstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pearl.oauth2.authorizationserverdemo.oauth2.PearlOauth2Properties;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.core.log.LogMessage;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.core.authority.mapping.NullAuthoritiesMapper;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.cache.NullUserCache;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.InvalidRequestException;
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
import org.springframework.security.oauth2.common.util.OAuth2Utils;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestValidator;
import org.springframework.security.web.authentication.NullRememberMeServices;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.www.BasicAuthenticationConverter;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
import java.util.Collections;
import java.util.Map;

*/
/**
 * Created by TD on 2021/7/15
 *//*

@RequiredArgsConstructor
@RestController
@RequestMapping("/pearl")
public class LoginController {

    protected final Log logger = LogFactory.getLog(this.getClass());

    private final ClientDetailsService clientDetailsService;

    private final BasicAuthenticationConverter basicAuthenticationConverter;

    private final RememberMeServices rememberMeServices = new NullRememberMeServices();

    private OAuth2RequestValidator oAuth2RequestValidator = new DefaultOAuth2RequestValidator();

    private final AuthorizationServerEndpointsConfiguration conf;



    private final PearlOauth2Properties pearlOauth2Properties;

    @PostMapping(value = "/login")
    @ResponseBody
    public ResponseEntity<OAuth2AccessToken> doLogin(@RequestParam Map<String, String> parameters, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // 1. 客户端认证
        UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(token.substring(0, delim), token.substring(delim + 1));
        try {
            UsernamePasswordAuthenticationToken authRequest = this.basicAuthenticationConverter.convert(request);
            String username = authRequest.getName();
            Authentication principal = null;
            this.logger.trace(LogMessage.format("Found username '%s' in Basic Authorization header", username));
            if (this.authenticationIsRequired(username)) {
                principal = authenticate(authRequest);
                SecurityContextHolder.getContext().setAuthentication(principal);
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug(LogMessage.format("Set SecurityContextHolder to %s", principal));
                }

                this.rememberMeServices.loginSuccess(request, response, principal);
                this.onSuccessfulAuthentication(request, response, principal);
            }

            // 2. 用户密码认证
            // 1. 校验Principal是否是认证信息





            // 8. 调用令牌发放器，颁发令牌
            OAuth2AccessToken token = conf.getEndpointsConfigurer().getTokenGranter().grant(tokenRequest.getGrantType(), tokenRequest);
            if (token == null) {
                throw new UnsupportedGrantTypeException("Unsupported grant type: " + tokenRequest.getGrantType());
            }
            // 9. 返回给客户端
            return getResponse(token);

        } catch (AuthenticationException var7) {
            SecurityContextHolder.clearContext();
            this.logger.debug("Failed to process authentication request", var7);
            this.rememberMeServices.loginFail(request, response);
            this.onUnsuccessfulAuthentication(request, response, var7);
            return null;
        }

    }

    private boolean authenticationIsRequired(String username) {
        Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication();
        if (existingAuth != null && existingAuth.isAuthenticated()) {
            return existingAuth instanceof UsernamePasswordAuthenticationToken && !existingAuth.getName().equals(username) || existingAuth instanceof AnonymousAuthenticationToken;
        } else {
            return true;
        }
    }


    protected void onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException {
    }


    protected void onUnsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException {
    }

    */
/**
     * @param principal the currently authentication principal
     * @return a client id if there is one in the principal
     *//*

    protected String getClientId(Principal principal) {
        Authentication client = (Authentication) principal;
        if (!client.isAuthenticated()) {
            throw new InsufficientAuthenticationException("The client is not authenticated.");
        }
        String clientId = client.getName();
        if (client instanceof OAuth2Authentication) {
            // Might be a client and user combined authentication
            clientId = ((OAuth2Authentication) client).getOAuth2Request().getClientId();
        }
        return clientId;
    }











    private boolean isRefreshTokenRequest(Map<String, String> parameters) {
        return "refresh_token".equals(parameters.get("grant_type")) && parameters.get("refresh_token") != null;
    }

    private boolean isAuthCodeRequest(Map<String, String> parameters) {
        return "authorization_code".equals(parameters.get("grant_type")) && parameters.get("code") != null;
    }


}
*/
